package com.example.administrator.jinritoutiao;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

import com.example.administrator.jinritoutiao.activity.GuideAvtivity;
import com.example.administrator.jinritoutiao.activity.HomeActivity;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private PackageManager packageManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView iv = (ImageView) findViewById(R.id.iv);
        SharedPreferences sp = getSharedPreferences("sp", MODE_PRIVATE);
        boolean flag = sp.getBoolean("flag", true);
        if (flag) {
            Intent intent = new Intent(this, GuideAvtivity.class);
            startActivity(intent);
            sp.edit().putBoolean("flag", false).commit();
        } else {
            packageManager = getPackageManager();
            try {
                PackageInfo info = packageManager.getPackageInfo("com.example.administrator.jinritoutiao", 0);
                int version = info.versionCode;
                if (version == 1) {
                    flash();
                }else{
                    AlertDialog dialog = new AlertDialog.Builder(this).create();
                    dialog.setTitle("更新");
                    dialog.setMessage("新增NB功能");
                }
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
    private void flash() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Intent intent = new Intent(MainActivity.this,HomeActivity.class);
                startActivity(intent);
                finish();
            }
        },3000);
    }
}
